package com.jing.shop.service.serviceImpl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.shaded.com.google.gson.JsonObject;
import com.google.common.collect.Lists;
import com.jing.common.core.utils.StringUtils;
import com.jing.shop.domain.dto.ProductShopDTO;
import com.jing.shop.domain.po.ProductAttributeCategoryPO;
import com.jing.shop.domain.po.ProductAttributePO;
import com.jing.shop.domain.vo.PmsProductAttrRelVO;
import com.jing.shop.domain.vo.ProductAttributeCategoryVO;
import com.jing.shop.domain.vo.ProductShopCategoryVO;
import com.jing.shop.domain.vo.ProductShopVO;
import com.jing.shop.mapper.ProductShopCategoryMapper;
import com.jing.shop.service.ProductShopCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class ProductShopCategoryServiceImpl implements ProductShopCategoryService {

    @Autowired
    private ProductShopCategoryMapper productShopCategoryMapper;

    @Override
    public List<ProductAttributeCategoryVO> findCategory() {
        List<ProductAttributeCategoryVO> category = productShopCategoryMapper.findCategory();
        return category;
    }

    @Override
    public List<ProductShopCategoryVO> findByCid(Integer cid,Integer type){
        List<ProductAttributePO> list = productShopCategoryMapper.findByCid(cid, type);
        return changeCategoryValueVo(list);
    }

    private List<ProductShopCategoryVO> changeCategoryValueVo(List<ProductAttributePO> list) {
        ArrayList<ProductShopCategoryVO> objects = new ArrayList<>();
        for (ProductAttributePO productAttributePO : list) {
            ProductShopCategoryVO productShopCategoryVO = new ProductShopCategoryVO();
            productShopCategoryVO.setAttributeName(productAttributePO.getAttributeName());
            productShopCategoryVO.setId(productAttributePO.getId());
            if(StringUtils.isNotEmpty((productAttributePO.getInputList()))){
                String[] split = productAttributePO.getInputList().split("，");
                List<String> valueList = Arrays.stream(split).collect(Collectors.toList());
                productShopCategoryVO.setInputList(valueList);

                ArrayList<PmsProductAttrRelVO> relVos = new ArrayList<>();
                for (String attrValue : valueList) {
                    PmsProductAttrRelVO pmsProductAttrRelVO = new PmsProductAttrRelVO();
                    pmsProductAttrRelVO.setAttrName(attrValue);
                    pmsProductAttrRelVO.setCid(productShopCategoryVO.getId());
                    relVos.add(pmsProductAttrRelVO);
                }
                productShopCategoryVO.setObjList(relVos);
            }
            objects.add(productShopCategoryVO);
        }
        return objects;
    }

    @Override
    public List<ProductShopVO> findServiceId() {
        List<ProductShopDTO> serviceId = productShopCategoryMapper.findServiceId();
        List<ProductShopVO> getallservice=getallservice(serviceId);
        return getallservice;
    }

    @Override
    public List<Map<Long, String>> changeAttrRelToSku(List<PmsProductAttrRelVO> pmsProductAttrRelVO) {
        if(CollectionUtils.isEmpty(pmsProductAttrRelVO)){
            throw new RuntimeException("参数不能为空");
        }
        Map<Long, List<PmsProductAttrRelVO>> groupedByCid = pmsProductAttrRelVO.stream()
                .collect(Collectors.groupingBy(PmsProductAttrRelVO::getCid));

        List<String> descartes = descartes(new ArrayList<>(groupedByCid.values()));

        List<Map<Long, String>> collect = descartes.stream()
                .map(descarte -> {
                    String[] split = descarte.split("--");
                    return Arrays.stream(split)
                            .map(s -> com.alibaba.fastjson2.JSONObject.parseObject(s, PmsProductAttrRelVO.class))
                            .collect(Collectors.toMap(PmsProductAttrRelVO::getCid, PmsProductAttrRelVO::getAttrName));
                })
                .collect(Collectors.toList());
        return collect;
    }


    public List<String> descartes(List<List<PmsProductAttrRelVO>> lists){
        List<String> result=Lists.cartesianProduct(lists).stream()
                .map(lst -> lst.stream().map(JSONObject::toJSONString).collect(Collectors.joining("--")))
                .collect(Collectors.toList());
        return result;
    }

    private List<ProductShopVO> getallservice(List<ProductShopDTO> serviceId) {
        ArrayList<ProductShopVO> list = new ArrayList<>();
        for (ProductShopVO productShopVO : list) {
            ProductShopVO productShopVO1 = new ProductShopVO();
            String[] split = productShopVO.getServiceIds().split("，");
            productShopVO1.setServiceId(Arrays.stream(split)
                    .collect(Collectors.toList()));
//            list.add(productShopVO1);
        }
        return list;
    }

    private List<ProductShopCategoryVO> getall(List<ProductAttributePO> list) {
        ArrayList<ProductShopCategoryVO> arrayList = new ArrayList<>();
        for (ProductAttributePO productAttributePO : list) {
            ProductShopCategoryVO productShopCategoryVO = new ProductShopCategoryVO();
            productShopCategoryVO.setId(productAttributePO.getId());
            productShopCategoryVO.setAttributeName(productAttributePO.getAttributeName());
            if(StringUtils.isNotEmpty(productAttributePO.getInputList())){
                String[] split = productAttributePO.getInputList().split("，");
                productShopCategoryVO.setInputList(Arrays.stream(split)
                        .collect(Collectors.toList()));
            }
            arrayList.add(productShopCategoryVO);
        }
        return arrayList;
    }

}
